package sng.modules.view.components.city.logic.attachment
{
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import sng.modules.model.vo.UserBuildingVO;
	import sng.modules.view.components.city.logic.unit.BaseBuilding;
	import sng.utils.TimeUtil;
	
	public class BuildingTipManager
	{
		private static var _instance:BuildingTipManager;
		
		private var container:Sprite;
		private var countdownTimer:Timer;//建筑倒计时 计时器
		private var uvo:UserBuildingVO;
		private var tip:BuildingTooltip;
		private var building:BaseBuilding;
		
		public function BuildingTipManager(enf:SingletonEnforcer, _container:Sprite)
		{
			container = _container;
		}
		
		//单例创建的方法
		public static function getInstance(_container:Sprite=null):BuildingTipManager{
			if(_instance == null) _instance = new BuildingTipManager(new SingletonEnforcer(), _container);
			return _instance;
		}
		
		//显示建筑的悬浮提示
		public function showTip(_building:BaseBuilding):void
		{
			if(container==null) return;
			building = _building;
			uvo = building.uVO;
			tip = new BuildingTooltip();
			var bPoint:Point = new Point(0, -60);
			bPoint = building.skin.localToGlobal(bPoint);
			tip.x = bPoint.x;
			tip.y = bPoint.y;
			tip.stop();
			
			//根据不同建筑、不同状态来实现
			//住宅
//			switch(building.currentState.toString())
//			{
//				case "BuildNewState":
//				{
//					tip.gotoAndStop("building_create");
//					break;
//				}
//				case "BuildNormalState":
//				{
//					tip.gotoAndStop("house");
//					var time:Number = TimeUtil.getTimeSecond();
//					tip.populationText.text = uvo.population+"/"+building.vo.populationUpLimit;
//					if(uvo.countdownTime>time)
//					{
//						tip.timeText.text = TimeUtil.getTimeStringFromSecond(uvo.countdownTime - time);
//						timerStart();
//					}
//					break;
//				}
//				case "BuildGatherState":
//				{
//					tip.gotoAndStop("building_gathing");
//					break;
//				}
//					
//				default:
//				{
//					break;
//				}
//			}
			tip.buildingNameText.text = building.vo.buildingname;
			
			container.addChild(tip);
		}
		
		//隐藏建筑的悬浮提示
		public function hideTip():void
		{
			if(container==null) return;
			while(container.numChildren>0)
			{
				container.removeChildAt(container.numChildren-1);
			}
			tip = null;
			timerStop();
		}
		
		public function timerStart():void
		{
			if(countdownTimer==null)
				countdownTimer = new Timer(1000);
			countdownTimer.addEventListener(TimerEvent.TIMER, timerHandler);
			countdownTimer.start();
		}
		
		protected function timerHandler(event:TimerEvent):void
		{
			var time:Number = TimeUtil.getTimeSecond();
			tip.timeText.text = TimeUtil.getTimeStringFromSecond(uvo.countdownTime-time);
			if(uvo!=null && uvo.countdownTime<time)
			{
				timerStop();
			}
		}
		
		public function timerStop():void
		{
			if(countdownTimer==null) return;
			countdownTimer.stop();
			countdownTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
			countdownTimer = null;
			uvo = null;
		}
	}
}

class SingletonEnforcer{};